package com.fc.tms.dao;

import java.io.Serializable;
import java.util.List;

public class DataPage<T> implements Serializable {

    private static final long serialVersionUID = 1185419751178318062L;

    Integer pageNumListSize = 9;

    //总共有多少行
    Integer rowCount = 0;

    //当前页码
    private Integer pageIndex = 1;

    //每页多少条记录
    private Integer pageSize = 10;

    //总共有多少页
    private Integer pageCount = 0;

    private List<T> dataList; //用户存储当前查询结果的集合

    private int[] indexes = new int[0];

    public DataPage(Integer rowCount, Integer pageIndex, Integer pageSize, List<T> dataList) {
        this.rowCount = rowCount;
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.dataList = dataList;

        this.init();
    }

    public void init() {
        this.pageCount = rowCount % pageSize == 0 ? rowCount / pageSize : rowCount / pageSize + 1;

        buildIndexs();
    }

    public boolean hasPretPage() {
        return pageIndex > 1;
    }

    public boolean hasNextPage() {
        return pageCount > pageIndex;
    }

    public Integer getFirstPage() {
        return 1;
    }

    public Integer getLastPage() {
        return this.pageCount;
    }

    public int[] buildIndexs() {
        if (pageNumListSize >= pageCount) {
            indexes = new int[pageCount];
            for (int i = 1; i <= pageCount; i++) {
                indexes[i - 1] = i;
            }
        } else {
            indexes = new int[pageNumListSize + 2];

            int leftSize = pageNumListSize / 2;

            int i = 0;
            indexes[i] = 1;

            int startNum = pageIndex - leftSize + 1;
            if (startNum < 2)
                startNum = 2;

            int manNum = pageIndex + leftSize;

            if (manNum <= startNum + pageNumListSize - 1)
                manNum = startNum + pageNumListSize - 1;

            if (manNum > pageCount - 1) {
                manNum = pageCount - 1;
                startNum = pageCount - pageNumListSize;
            }

            for (; startNum <= manNum; startNum++) {
                indexes[++i] = startNum;
            }

            indexes[++i] = pageCount;
        }
        return indexes;
    }

    public int[] getIndexes() {
        return indexes;
    }

    public void setIndexes(int[] indexes) {
        this.indexes = indexes;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }

    public static void main(String[] args) {
    }
}